/**
 * 
 */
package com.nari.heatshare.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.heatshare.model.ITemperaturePoint;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.rescontroller.dao.IResourceAccessControlDao;

/**
 * 
 * @author Xiaopeng
 * @date 2014-3-11
 */
public class TemperaturePointDaoImpl extends GenericDaoImpl<ITemperaturePoint, String> 
		implements ITemperaturePointDao {
	private IResourceAccessControlDao iResourceAccessControlDao;
	
	/**
	 * 分页查询温度点列表
	 * @param seriousNum 温度点编号(可选)
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param concentratorUID 所属集中器(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-11
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<ITemperaturePoint> queryTempPointPage(String seriousNum, 
			String projectUID, String communityUID, String buildingUID, String concentratorUID, 
			PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(ITemperaturePoint.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(ITemperaturePoint.class);
		//温度点编号
		if (seriousNum!=null && !"".equals(seriousNum.trim())) {
			listCriteria.add(Restrictions.ilike("seriousNum", seriousNum.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("seriousNum", seriousNum.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//所属项目
		if (projectUID!=null && !"".equals(projectUID.trim())) {
			listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		//所属小区
		if (communityUID!=null && !"".equals(communityUID.trim())) {
			listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
		}
		//所属楼栋
		if (buildingUID!=null && !"".equals(buildingUID.trim())) {
			listCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
			countCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
		}
		//所属集中器
		if (concentratorUID!=null && !"".equals(concentratorUID.trim())) {
			listCriteria.add(Restrictions.eq("concentratorID", concentratorUID.trim()));
			countCriteria.add(Restrictions.eq("concentratorID", concentratorUID.trim()));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		//统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		//结果集排序
		listCriteria.addOrder(Order.asc("concentratorNo")).addOrder(Order.asc("seriousNum"));
		//结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<ITemperaturePoint> tempPointPage = new ResultWithPageCond<ITemperaturePoint>();
		tempPointPage.setList(listCriteria.list());
		tempPointPage.setPageCond(pageCond);
		return tempPointPage;
	}
	
	/**
	 * 统计同一集中器下某个温度点编号的设备个数
	 * 说明：不包含指定UID的温度点设备
	 * @param tempPointUID
	 * @param concentratorUID
	 * @param seriousNum
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public int countTempPointNumByConcentrator(String tempPointUID, String concentratorUID, 
			String seriousNum) throws Exception {
		String hsql = "select count(*) " +
				"from ITemperaturePoint tp " +
				"where tp.concentratorID='"+concentratorUID+"' " +
				"and tp.seriousNum='"+seriousNum+"'";
		if(tempPointUID!=null && !"".equals(tempPointUID)) {
			hsql += " and tp.id<>'"+tempPointUID+"'";
		}
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}
	
	/**
	 * 通过集中器所属的温度点编号查询温度点信息
	 * 说明：原则上，同一集中器下温度点编号唯一
	 * @param concentratorNo
	 * @param seriousNum
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-13
	 */
	public ITemperaturePoint queryTempPointBySeriousNum(String concentratorNo, 
			String seriousNum) throws Exception {
		ITemperaturePoint temperaturePoint = null;
		String hsql = "from ITemperaturePoint tp " +
				"where tp.concentratorNo='"+concentratorNo+"' " +
				"and tp.seriousNum='"+seriousNum+"'";
		List<ITemperaturePoint> tempPointList = this.runQueryHSql(hsql);
		if(tempPointList!=null && tempPointList.size()>0) {
			temperaturePoint = tempPointList.get(0);
		}
		return temperaturePoint;
	}
	
	/**
	 * 统计集中器编号统计其所属的温度点个数
	 * @param concentratorNo
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public int countTempPointsByConcentratorNo(String concentratorNo) throws Exception {
		String hsql = "select count(*) " +
				"from ITemperaturePoint tp " +
				"where tp.concentratorNo='"+concentratorNo+"'";
		return ((Long)this.getCurrentSession().createQuery(hsql).uniqueResult()).intValue();
	}
	
	/**
	 * 通过温度点所属关系统计温度点的个数
	 * @param projectUID
	 * @param communityUID
	 * @param buildingUID
	 * @param concentratorID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public int countTempPointSumByBelongs(String projectUID, String communityUID, 
			String buildingUID, String concentratorUID) throws Exception {
		Criteria countCriteria = this.getCurrentSession().createCriteria(ITemperaturePoint.class);
		//所属项目
		if (projectUID!=null && !"".equals(projectUID.trim())) {
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		//所属小区
		if (communityUID!=null && !"".equals(communityUID.trim())) {
			countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
		}
		//所属楼栋
		if (buildingUID!=null && !"".equals(buildingUID.trim())) {
			countCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
		}
		//所属集中器
		if (concentratorUID!=null && !"".equals(concentratorUID.trim())) {
			countCriteria.add(Restrictions.eq("concentratorID", concentratorUID.trim()));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		//统计结果集个数
		return ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
	}
	
	/**
	 * 通过集中器唯一标识查询对应的温度点列表
	 * @param concentratorUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public List<ITemperaturePoint> queryTempPointsByConcentratorUID(String concentratorUID) throws Exception {
		String hsql = "from ITemperaturePoint tp " +
				"where tp.concentratorID='"+concentratorUID+"' " +
				"order by tp.seriousNum asc";
		return this.runQueryHSql(hsql);
	}
	
	/**
	 * 通过温度点所属关系查询(所属项目、所属小区、所属楼栋、集中器)查询温度点列表
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param concentratorUID 所属集中器(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-25
	 */
	@SuppressWarnings("unchecked")
	public List<ITemperaturePoint> queryTempPointListByBelongs(String projectUID, 
			String communityUID, String buildingUID, String concentratorUID) throws Exception {
		if((projectUID!=null && !"".equals(projectUID.trim())) 
				|| (communityUID!=null && !"".equals(communityUID.trim())) 
				|| (buildingUID!=null && !"".equals(buildingUID.trim()))
				|| (concentratorUID!=null && !"".equals(concentratorUID.trim()))) {
			Criteria listCriteria = this.getCurrentSession().createCriteria(ITemperaturePoint.class);
			//所属项目
			if(projectUID!=null && !"".equals(projectUID.trim())) {
				listCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
			}
			//所属小区
			if(communityUID!=null && !"".equals(communityUID.trim())) {
				listCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
			}
			//所属楼栋
			if(buildingUID!=null && !"".equals(buildingUID.trim())) {
				listCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
			}
			//所属集中器
			if(concentratorUID!=null && !"".equals(concentratorUID.trim())) {
				listCriteria.add(Restrictions.eq("concentratorID", concentratorUID.trim()));
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
			if(!"".equals(authorityRes)) {
				//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
				listCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
			}
			//结果集排序
			listCriteria.addOrder(Order.asc("seriousNum")).addOrder(Order.asc("concentratorID"));
			return listCriteria.list();
		}
		return new ArrayList<ITemperaturePoint>();
	}

	/**
	 * @return the iResourceAccessControlDao
	 */
	public IResourceAccessControlDao getiResourceAccessControlDao() {
		return iResourceAccessControlDao;
	}

	/**
	 * @param iResourceAccessControlDao the iResourceAccessControlDao to set
	 */
	public void setiResourceAccessControlDao(
			IResourceAccessControlDao iResourceAccessControlDao) {
		this.iResourceAccessControlDao = iResourceAccessControlDao;
	}
	
}
